﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AppConfigManager.cs" company="" />
// <summary>
//   Manages configuration for an application.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Utilities
{
    using System.Configuration;

    /// <summary>
    /// Manages configuration for an application.
    /// </summary>
    /// <typeparam name="TConfig">The type of the configuration section managed by this manager.</typeparam>
    public class AppConfigManager<TConfig>
        where TConfig : ConfigurationSection
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AppConfigManager{TConfig}"/> class and creates the configuration section representation.
        /// </summary>
        /// <param name="sectionName">The string representation of the configuration section in the configuration file.</param>
        public AppConfigManager(string sectionName)
        {
            var configuration = ConfigurationManager.GetSection(sectionName) as TConfig;

            if (configuration == null)
            {
                throw new ConfigurationErrorsException("Wrong configuration section type.");
            }

            Configuration = configuration;
        }

        /// <summary>
        /// Gets the configuration representation.
        /// </summary>
        public TConfig Configuration { get; private set; }
    }
}
